package com.shanghaionstar.dealersorder.adapter;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.base.CommonAdapter;
import com.onstar.cn.framework.base.CommonViewHolder;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.PermissionCall;
import com.onstar.cn.mag.dealer.MaintenanceDealerResponseEntity;
import com.shanghaionstar.R;
import com.shanghaionstar.dealersorder.DealerPoiMapActivity;
import com.shanghaionstar.dealersorder.OrderActivity;

import java.util.List;

/**
 * @ClassName: DealerBookingListAdapter
 * @Description: 经销商预约列表适配器 <br/>
 * dealer booking list adapter
 */
public class DealersOrderAdapter extends CommonAdapter<MaintenanceDealerResponseEntity> {

    private BaseActivity baseActivity;

    public DealersOrderAdapter(List<MaintenanceDealerResponseEntity> datas, BaseActivity context) {
        super(datas, context);
        this.baseActivity = context;
    }

    public void setDealerBookingLists(List<MaintenanceDealerResponseEntity> dealerBookingLists) {
        this.datas = dealerBookingLists;
        notifyDataSetChanged();
    }

    @Override
    public void getView(final int position, CommonViewHolder viewHolder) {
        final ViewHolder holder = (ViewHolder) viewHolder;
        final MaintenanceDealerResponseEntity dealerResponseEntity = datas.get(position);
        holder.dealer_company_name.setText(dealerResponseEntity.getName());
        holder.dealer_address_info.setText(dealerResponseEntity.getServiceAddress());
        holder.dealer_space.setText(dealerResponseEntity.getDistance() + "km");
        String phone;
        if (dealerResponseEntity.getServiceTel().contains(";")) {
            phone = dealerResponseEntity.getServiceTel().split(";")[0];
        } else {
            phone = dealerResponseEntity.getServiceTel().trim();
        }
        if (CommonUtil.isNull(phone)) {
            holder.btn_call_dealer.setText(baseActivity.getResources().getString(R.string.nav_no_phone));
            holder.btn_call_dealer.setTextColor(ContextCompat.getColor(baseActivity, R.color.hint_color));
            holder.btn_call_dealer.setClickable(false);
            holder.btn_call_dealer.setEnabled(false);
        } else {
            holder.btn_call_dealer.setText(phone);
            holder.btn_call_dealer.setTextColor(ContextCompat.getColor(baseActivity, R.color.font_flow_price));
            holder.btn_call_dealer.setClickable(true);
            holder.btn_call_dealer.setEnabled(true);
        }
        //拨打电话
        holder.btn_call_dealer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!CommonUtil.isNull(dealerResponseEntity.getServiceTel())) {
                    baseActivity.requestRunTimePermissions(new String[]{Manifest.permission.CALL_PHONE}, new PermissionCall() {
                        @Override
                        public void requestSuccess() {
                            Intent callIntent = new Intent(
                                    Intent.ACTION_CALL,
                                    Uri.parse("tel:" + holder.btn_call_dealer.getText().toString().trim()));
                            baseActivity.startActivity(callIntent);
                        }

                        @Override
                        public void refused() {

                        }
                    });

                }
            }
        });
        //查看地图
        holder.btn_showtomap.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, DealerPoiMapActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("dealerResponse", dealerResponseEntity);
                intent.putExtras(bundle);
                baseActivity.startActivity(intent);
            }
        });
        holder.dealer_list_item_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(baseActivity, OrderActivity.class);
                Bundle args = new Bundle();
                args.putSerializable("dealerResponse", dealerResponseEntity);
                intent.putExtras(args);
                baseActivity.startActivity(intent);
            }
        });
    }

    @Override
    public CommonViewHolder getViewHolder() {
        return new ViewHolder();
    }

    @Override
    public int layoutId() {
        return R.layout.onstar_dealer_list_item;
    }

    private class ViewHolder extends CommonViewHolder {
        @FindViewById
        TextView dealer_company_name;// 经销商名称
        @FindViewById
        TextView dealer_address_info;// 经销商地址信息
        @FindViewById
        TextView dealer_space;// 经销商距离
        @FindViewById
        Button btn_showtomap;// 查看地图
        @FindViewById
        Button btn_call_dealer;//拨打电话
        @FindViewById
        View dealer_list_item_view;
    }
}
